<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class OxMenu 
{
	public $text;
    public $file;
    public $level;
    public $initialy_expanded;
    
    private $parent;
    private $children;
    
    /* @return OxMenu[] */
    public function Children()
    {
        
        return $this->children;
    }
    
    public function __construct($s="", $f="", $ie = true)
    {
        $this->parent = null;
        $this->children = array();
        $this->text = $s;
        $this->file = $f;
        if($s && $f)
        {
            //we have to allow this for the loader
            //debug_break();            
        }
        $this->level = 0;
        $this->initialy_expanded = $ie;
        
	}    
    
     /* @return OxMenu */
    public static function new_link_menu($url, $text="", $class="")
    {
        //allow for both local and global urls
        if(strpos($url, '//')===false)
        {
            $url = site_url($url);
        }
        
    
        if(!$text) { $text = $url; }       
    
        $s = '<a ';
        if($class) { $s .= 'class="'.$class.'" '; }
        $s .= 'href="'.$url.'">'.$text.'</a>';
    

        return new OxMenu($s);
    }     
    
    public static function new_text_menu($text, $initialy_expanded = true)
    {
        return new OxMenu($text, '', $initialy_expanded);
    }     
    public static function new_menu_from_file($view_file_name)
    {
        return new OxMenu("", $view_file_name);
    }    
    public function new_child_link_menu($url, $text="", $class="")
    {
        $m = OxMenu::new_link_menu($url, $text, $class);
        $m->parent = $this;
        $this->children[] = $m;
        $m->level = $this->level + 1;

        return $this;
    }     
    
    public function new_child_text_menu($text)
    {
        $m = OxMenu::new_text_menu($text);
        $m->parent = $this;
        $this->children[] = $m;
        $m->level = $this->level + 1;

        return $this;        
    }     
    public function new_child_menu_from_file($view_file_name)
    {
        $m = OxMenu::new_menu_from_file($view_file_name);
        $m->parent = $this;
        $this->children[] = $m;
        $m->level = $this->level + 1;

        return $this; 
    }    
    
    public function show_menu($subtime_id = 0)
    {
        $s = '<div class="menu_item ';
        if($this->level > 0) { $s .= 'menu_subitem'; }
        $s .= ' menu_level_' . $this->level . '" id="menu_' . $this->level . '_' . $subtime_id . '">';
        
        if($this->children)
        {
            $s .= '<span class="menu_item_pre">';
            $s .= "+";
            //Following is disabled, because the expand script would not change it.
            /*
            if($this->initialy_expanded)
            {
                $s .= "+";
            }
            else
            {
                $s .= "-";
            }
             * 
             */
            $s .= '</span>';
            
        }
        if($this->text)
        {
            //If it has submenus, then it should be link to the javascript command for expansion
            if($this->children)
            {
                $s .= '<a href="javascript:menu_expansion(\'menu_' 
                    . $this->level . '_' . $subtime_id
                    .'\')">' . $this->text . '</a>';
            }
            else
            {
                $s .= $this->text; 
            }
        }
        else
        {
            $s .= $ctrl_obj->load->view($this->file, '', true);
        }
            
        $s .= '</div>';
        
        
        
        //now subitems
        if($this->children)
        {
            $s .= '<div class="submenu submenu_level_' . $this->level . '" '
                    .'id="submenu_' . $this->level . '_' . $subtime_id . '" '
                    .'style="padding: '. (($this->level+1) * 8) .'px;';
            if(!$this->initialy_expanded)
            {
                $s .= ' display:none;';
            }
            $s .= '">';
            $i = 0;
            foreach($this->children as $child)
            {
                /* @var $child OxMenu */
                $s .= $child->show_menu($i);
                $i++;
            }
            $s .= '</div>';
        }
        
        return $s;
    }
}